package com.example.common.exception;


import lombok.Getter;

import java.io.Serializable;

/**
 * 基础异常
 */
public class BaseException extends RuntimeException implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    @Getter
    private int code;

    /**
     * 错误消息
     */
    @Getter
    private String msg;

    /**
     * 详细信息
     */
    @Getter
    private String details;

    public BaseException(String msg) {
        super(msg);
        this.msg = msg;
        this.details = msg;
    }

    public BaseException(String msg, Throwable e) {
        super(msg, e);
        this.msg = msg;
        this.details = msg;
    }

    public BaseException(int code, String msg) {
        super(code + "," + msg);
        this.code = code;
        this.msg = msg;
        this.details = msg;
    }

    public BaseException(int code, String msg, String details) {
        super(code + "," + msg + "," + details);
        this.code = code;
        this.msg = msg;
        this.details = details;
    }

    public BaseException(int code, String msg, Throwable e) {
        super(code + "," + msg, e);
        this.code = code;
        this.msg = msg;
        this.details = msg;
    }

    public BaseException(ErrorMsgEnum errCode) {
        super(errCode.getMsg());
        this.code = errCode.getCode();
        this.msg = errCode.getMsg();
        this.details = msg;
    }

    public BaseException(ExceptionCode exceptionCode) {
        super(exceptionCode.getMsg());
        this.code = exceptionCode.getCode();
        this.msg = exceptionCode.getMsg();
        this.details = msg;
    }

    public BaseException(ExceptionCode exceptionCode, String details) {
        super(String.format("%d,%s,%s", exceptionCode.getCode(), exceptionCode.getMsg(), details));
        this.code = exceptionCode.getCode();
        this.msg = exceptionCode.getMsg();
        this.details = details;
    }

    @Override
    public String toString() {
        return String.format("%d %s:%s", code, msg, details);
    }
}
